package com.ctshk.sapp.train.controller;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.annotations.Authority;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.train.dto.*;
import com.ctshk.rpc.train.req.*;
import com.ctshk.rpc.train.service.CJTrainProductService;
import com.ctshk.rpc.train.service.ITrainProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 火车产品表 前端控制器
 * </p>
 *
 * @author 彭柳霖
 * @since 2021-03-11
 */
@Api(value="资源中心-火车产品接口", tags="资源中心-火车产品接口")
@RestController
@RequestMapping("/train-product")
public class TrainProductController extends BaseController {

    @DubboReference
    private ITrainProductService trainProductService;

    @DubboReference
    private CJTrainProductService cjTrainProductService;

    @ApiOperation(value = "资源中心-火车票产品管理列表查询", notes = "资源中心-火车票产品管理列表查询")
    @PostMapping(value = "/query-list")
    @Authority
    public Result<PageResponse<TrainProductPageDTO>> queryList(@RequestBody @Valid TrainProductPageReq trainProductPageReq) {
        TokenUser currentUser = getCurrentUser();
        trainProductPageReq.setCurrentUserId(currentUser.getId());
        return Result.success(trainProductService.queryList(trainProductPageReq));
    }

    @ApiOperation(value="新增火车票产品", notes="新增火车票产品")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.ADD, operTitle = "新增火车票产品", operDesc = "新增火车票产品")
    @PostMapping("/save")
    public Result saveTrainProduct(@RequestBody @Valid TrainProductReq trainProductReq) {
        TokenUser user = getCurrentUser(getRequest());
        return trainProductService.saveTrainProduct(trainProductReq,user);
    }

    @ApiOperation(value="新增火车预订信息", notes="新增火车产品预订信息")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.ADD, operTitle = "新增火车产品预订信息", operDesc = "新增火车产品预订信息")
    @PostMapping("/saveReservation")
    public Result saveReservation(@RequestBody @Valid TrainProductReservationSettingsReq reservationSettingsReq) {
        TokenUser user = getCurrentUser(getRequest());
        return trainProductService.saveReservation(reservationSettingsReq,user);
    }

    @ApiOperation(value="修改火车预订信息", notes="修改火车预订信息")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.UPDATE, operTitle = "修改火车产品预订信息", operDesc = "修改火车产品预订信息")
    @PostMapping("/updateReservation")
    public Result updateReservation(@RequestBody @Valid TrainProductReservationSettingsReq reservationSettingsReq) {
        TokenUser user = getCurrentUser(getRequest());
        return trainProductService.updateReservation(reservationSettingsReq,user);
    }


    @ApiOperation(value = "新增-编辑提交审批", notes = "新增-编辑提交审批")
    @GetMapping(value = "/update-approval-status/{id}")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.UPDATE, operTitle = "火车票产品提交审批", operDesc = "火车票产品提交审批")
    public Result submit(@PathVariable("id") Long id) {
        TokenUser user = getCurrentUser(getRequest());
        return trainProductService.submit(id,user);
    }


    @ApiOperation(value="修改审核通过火车票产品", notes="修改审核通过火车票产品")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.UPDATE, operTitle = "修改审核通过火车票产品", operDesc = "修改审核通过火车票产品")
    @PostMapping("/approved/update")
    public Result updateApprovedBusProduct(@RequestBody @Valid TrainProductReq trainProductReq) {
        TokenUser user = getCurrentUser(getRequest());
        return trainProductService.updateApprovedTrainProduct(trainProductReq,user);
    }

    @ApiOperation(value="修改审核不通过火车票产品", notes="修改审核不通过火车票产品")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.UPDATE, operTitle = "修改审核不通过火车票产品", operDesc = "修改审核不通过火车票产品")
    @PostMapping("/unapproved/update")
    public Result updateUnapprovedBusProduct(@RequestBody @Valid TrainProductReq trainProductReq) {
        TokenUser user = getCurrentUser(getRequest());
        return trainProductService.updateUnapprovedTrainProduct(trainProductReq,user);
    }


    @ApiOperation(value = "编辑时查询火车产品详情", notes = "编辑时查询火车产品详情")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.QUERY, operTitle = "编辑时查询火车产品详情", operDesc = "编辑时查询火车产品详情")
    @GetMapping(value = "/detail")
    public Result<TrainProductDTO> queryDetail(@RequestParam(value = "id", required = true) Long id) {
        return Result.success(trainProductService.queryDetail(id));
    }

    @ApiOperation(value = "API设置", notes = "API设置")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.UPDATE, operTitle = "API设置", operDesc = "API设置")
    @PostMapping(value = "/setApi")
    public Result setApi(@RequestBody @Valid TrainProductSetReq trainProductSetReq) {
        TokenUser user = getCurrentUser(getRequest());
        return trainProductService.setApi(trainProductSetReq,user);

    }

    @ApiOperation(value = "有效无效设置", notes = "有效无效设置")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.UPDATE, operTitle = "有效无效设置", operDesc = "有效无效设置")
    @GetMapping("/setValid/{id}")
    public Result setValid(@PathVariable("id") Long id) {
        TokenUser user = getCurrentUser(getRequest());
        return trainProductService.setValid(id,user);

    }

    @ApiOperation(value = "批量设置", notes = "批量设置")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.UPDATE, operTitle = "有效无效设置", operDesc = "有效无效设置")
    @PostMapping(value = "/batchSetting")
    public Result batchSetting(@RequestBody @Valid BatchTrainProductInfoReq batchTrainProductInfoReq) {
        TokenUser user = getCurrentUser(getRequest());
        return trainProductService.batchSetting(batchTrainProductInfoReq,user);
    }

    @ApiOperation(value = "推荐设置", notes = "推荐设置")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.UPDATE, operTitle = "推荐设置", operDesc = "推荐设置")
    @GetMapping(value = "/setRecommended/{id}")
    public Result setRecommended(@PathVariable("id") Long id) {
        TokenUser user = getCurrentUser(getRequest());
        return trainProductService.setRecommended(id,user);

    }



    @ApiOperation(value = "查询火车票推荐列表", notes = "查询火车票推荐列表")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.QUERY, operTitle = "查询火车票推荐列表", operDesc = "查询火车票推荐列表")
    @PostMapping(value = "/recommendedBus/list")
    @Authority
    public Result<List<RecommendedTrainDTO>> queryRecommendedBusTicket(@RequestBody RecommendedBusTicketReq req) {
        Long userId = getCurrentUser(getRequest()).getId();
        List<RecommendedTrainDTO> reservationBusDTOList = trainProductService.queryRecommendedBusTicket(userId, req);
        return Result.success(reservationBusDTOList);

    }


    @ApiOperation(value = "火车票预订查询列表", notes = "火车票预订查询列表")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.QUERY, operTitle = "火车票预订查询列表", operDesc = "火车票预订查询列表")
    @PostMapping(value = "/reservationBus/list")
    @Authority
    public Result<PageResponse<ReservationTrainPageDTO>> queryReservationBusList(@RequestBody @Valid ReservationTrainPageReq req) {
        Long userId = getCurrentUser(getRequest()).getId();
        return Result.success(trainProductService.queryReservationBusList(req,userId));
    }

    @ApiOperation(value="获取旅客类型", notes="获取旅客类型")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.QUERY, operTitle = "获取旅客类型", operDesc = "获取旅客类型")
    @PostMapping("/query/touristtype")
    public Result queryTouristType(){
        return  Result.success(trainProductService.queryTouristType());
    }


    @ApiOperation("运营中心查询火车产品列表")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.QUERY, operTitle = "运营中心查询巴士产品列表", operDesc = "运营中心查询巴士产品列表")
    @PostMapping("queryProductListForIotc")
    public Result<PageResponse<TrainProductDTO>> queryProductListForIotc(@RequestBody TrainProductListForIotcQueryReq req) {
        return trainProductService.queryProductListForIotc(req);
    }

    @ApiOperation(value = "新增-审批", notes = "新增-审批(撤回,审批通过,审批不通过)",tags = "资源中心")
    @PostMapping(value = "/update-approval-check")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.UPDATE, operTitle = "门票售卖状态修改", operDesc = "门票售卖状态修改")
    public Result<Long> check(@RequestBody TrainProductCheckReq trainProductCheckReq) {
        return trainProductService.check(trainProductCheckReq,getCurrentUser(getRequest()));
    }

    @ApiOperation("查询预订用的代理商信息列表")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.QUERY, operTitle = "查询预订用的代理商信息列表", operDesc = "查询预订用的代理商信息列表")
    @PostMapping("queryReservationAgentList")
    public Result<List<TrainProductReservationAgentDTO>> queryReservationAgentList(@RequestBody @Valid TrainProductReservationAgentQueryReq req) {
        return trainProductService.queryReservationAgentList(req, getCurrentUser());
    }

    @GetMapping("/get_train_line_list")
    public Result test() {
        return Result.success(cjTrainProductService.getTrainLineList());
    }
}
